package com.example.cpl.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.sql.Date;
import java.text.SimpleDateFormat;

public class View2 extends AppCompatActivity {
      String filename1 = "MySharedString_View2_1",filename2 = "MySharedString_View2_2",filename3 = "MySharedString_View2_4";
        //数据库对应文件
    private Button but_sav;
    private EditText et_name,et_1,et_2,et_3,et_4,et_5,et_6,et_7,et_8;
    private ImageView img_who;

    private void initWidght() {
        et_1 = (EditText) findViewById(R.id.et_1);
        et_2 = (EditText) findViewById(R.id.et_2);
        et_5 = (EditText) findViewById(R.id.et_5);
        et_6 = (EditText) findViewById(R.id.et_6);
        et_7 = (EditText) findViewById(R.id.et_7);
        et_name = (EditText) findViewById(R.id.et_name);
        but_sav=(Button)findViewById(R.id.but_sav);
        img_who=findViewById(R.id.img_who);
    }
    private String change()
    {
        switch(MainActivity.who)
        {
            case 1: return filename1;
            case 2: return filename2;
            case 3: return filename3;
            default:
                return null;
        }

    }
    private void change_image()         //切换头像
    {
        switch(MainActivity.who)
        {
            case 1: img_who.setImageResource(R.drawable.who1); //设置图片（对应头像）//ldl\app\src\main\res\drawable
                break;
            case 2: img_who.setImageResource(R.drawable.who2); //设置图片（对应头像）
                break;
            case 3: img_who.setImageResource(R.drawable.who3); //设置图片（对应头像）
                break;
            default:
                break;

        }

    }

    public String read_SP_Str(String s,String s1) {
        final SharedPreferences someData = getSharedPreferences(change(), 0);
        return  someData.getString(""+s+"",""+s1+"");
    }
    public void  write_SP_Str(String s,String s1) {
        final SharedPreferences someData = getSharedPreferences(change(), 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putString(""+s+"",""+s1+"");
        editor.commit();// 提交修改
    }

    public String getTime(){
        long time=System.currentTimeMillis();   //long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format=new SimpleDateFormat("MM-dd HH:mm");
        Date d1=new Date(time);
        String t1=format.format(d1);
        Log.d("TAG",t1);
        return t1;
    }
    private void set_SP() {

        String ename="",   e1="",   e2="",   e3="",e4="",e5="",e6="",e7="",e8="";


        ename=read_SP_Str("et_name", "");
        e1=read_SP_Str("et_1", "");         //读取数据库值
        e2=read_SP_Str("et_2", "");
        e5=read_SP_Str("et_5", "");
        e6=read_SP_Str("et_6", "");
        e7=read_SP_Str("et_7", "");


        et_name.setText(ename);
        et_1.setText(e1);
        et_2.setText(e2);
        et_5.setText(e5);
        et_6.setText(e6);
        et_7.setText(e7);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view2);

        initWidght();
        change_image();// 头像切换
        set_SP();

        but_sav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                write_SP_Str("et_name", et_name.getText().toString());
                write_SP_Str("et_1", et_1.getText().toString());
                write_SP_Str("et_2", et_2.getText().toString());
                write_SP_Str("et_5", et_5.getText().toString());
                write_SP_Str("et_6", et_6.getText().toString());
                write_SP_Str("et_7", et_7.getText().toString());



                Toast.makeText(getApplicationContext(), "已保存", Toast.LENGTH_SHORT)
                        .show();//阴影提示框



            }
        });

    }
}
